package view.bookAdmiView;

import java.awt.EventQueue;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Vector;

import javax.swing.JInternalFrame;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.table.DefaultTableModel;

import dao.BookDao;
import dao.BookTypeDao;
import dao.BorrowImfDao;
import model.Book;
import model.BookType;
import util.DbUtil;
import util.StringUtil;

import javax.swing.JPanel;
import javax.swing.border.LineBorder;
import javax.swing.border.TitledBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JRadioButton;
import javax.swing.ButtonGroup;
import javax.swing.JTextArea;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Font;
import java.awt.Color;

public class BookManageInterFrm extends JInternalFrame {
	private JTable bookTable;
	private JTextField s_bookNameTxt;
	private JTextField s_authorTxt;
	private JComboBox s_bookTypeJcb;
	private JRadioButton manJrb;
	private JRadioButton femaleJrb ;
	private JTextArea bookDescTxt;
	private JComboBox bookTypeJcb;
	private JRadioButton notBorrowedJrb ;
	private JRadioButton borrowedJrb;
	private JRadioButton s_notBorrowedJrb;
	private JRadioButton s_borrowedJrb;
	
	private DbUtil dbUtil=new DbUtil();
	private BookTypeDao bookTypeDao=new BookTypeDao();
	private BookDao bookDao=new BookDao();
	
	private JTextField idTxt;
	private JTextField bookNameTxt;
	private final ButtonGroup buttonGroup = new ButtonGroup();
	private JTextField priceTxt;
	private JTextField authorTxt;
	private final ButtonGroup buttonGroup_1 = new ButtonGroup();
	private final ButtonGroup buttonGroup_2 = new ButtonGroup();
	private BorrowImfDao borrowImfDao=new BorrowImfDao();
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					BookManageInterFrm frame = new BookManageInterFrm();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public BookManageInterFrm() {
		setBackground(new Color(224, 255, 255));
		setClosable(true);
		setIconifiable(true);
		setTitle("图书管理");
		setBounds(100, 100, 1471, 898);
		
		JScrollPane bookScrollPane = new JScrollPane();
		
		JPanel bookSearchPanel = new JPanel();
		bookSearchPanel.setBackground(new Color(250, 235, 215));
		bookSearchPanel.setBorder(new TitledBorder(null, "\u641C\u7D22\u6761\u4EF6", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		
		JPanel bookMaintainPanel = new JPanel();
		bookMaintainPanel.setBackground(new Color(250, 235, 215));
		bookMaintainPanel.setBorder(new TitledBorder(null, "\u8868\u5355\u64CD\u4F5C", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		GroupLayout groupLayout = new GroupLayout(getContentPane());
		groupLayout.setHorizontalGroup(
			groupLayout.createParallelGroup(Alignment.TRAILING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(72)
							.addComponent(bookScrollPane, GroupLayout.DEFAULT_SIZE, 1369, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(72)
							.addComponent(bookMaintainPanel, GroupLayout.PREFERRED_SIZE, 1369, Short.MAX_VALUE))
						.addGroup(groupLayout.createSequentialGroup()
							.addGap(63)
							.addComponent(bookSearchPanel, GroupLayout.DEFAULT_SIZE, 1378, Short.MAX_VALUE)))
					.addContainerGap())
		);
		groupLayout.setVerticalGroup(
			groupLayout.createParallelGroup(Alignment.LEADING)
				.addGroup(groupLayout.createSequentialGroup()
					.addGap(30)
					.addComponent(bookSearchPanel, GroupLayout.PREFERRED_SIZE, 105, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(bookScrollPane, GroupLayout.PREFERRED_SIZE, 291, GroupLayout.PREFERRED_SIZE)
					.addGap(58)
					.addComponent(bookMaintainPanel, GroupLayout.DEFAULT_SIZE, 352, Short.MAX_VALUE)
					.addGap(142))
		);
		
		JLabel id = new JLabel("编号：");
		id.setFont(new Font("宋体", Font.BOLD, 25));
		
		idTxt = new JTextField();
		idTxt.setFont(new Font("宋体", Font.BOLD, 25));
		idTxt.setEditable(false);
		idTxt.setColumns(10);
		
		JLabel bookName = new JLabel("图书名称：");
		bookName.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookNameTxt = new JTextField();
		bookNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		bookNameTxt.setColumns(10);
		
		JLabel sex = new JLabel("作者性别：");
		sex.setFont(new Font("宋体", Font.BOLD, 25));
		
		manJrb = new JRadioButton("男");
		manJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup.add(manJrb);
		manJrb.setSelected(true);
		
		femaleJrb = new JRadioButton("女");
		femaleJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup.add(femaleJrb);
		
		JLabel price = new JLabel("价格：");
		price.setFont(new Font("宋体", Font.BOLD, 25));
		
		priceTxt = new JTextField();
		priceTxt.setFont(new Font("宋体", Font.BOLD, 25));
		priceTxt.setColumns(10);
		
		JLabel author = new JLabel("图书作者：");
		author.setFont(new Font("宋体", Font.BOLD, 25));
		
		authorTxt = new JTextField();
		authorTxt.setFont(new Font("宋体", Font.BOLD, 25));
		authorTxt.setColumns(10);
		
		JLabel bookType = new JLabel("图书类别：");
		bookType.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookTypeJcb = new JComboBox();
		bookTypeJcb.setFont(new Font("宋体", Font.BOLD, 25));
		
		JLabel bookDesc = new JLabel("图书描述：");
		bookDesc.setFont(new Font("宋体", Font.BOLD, 25));
		
		bookDescTxt = new JTextArea();
		bookDescTxt.setFont(new Font("宋体", Font.BOLD, 25));
		
		JButton alterButton_1 = new JButton("修改");
		alterButton_1.setFont(new Font("宋体", Font.BOLD, 25));
		alterButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				bookUpdateActionPerformed(evt);
			}
		});
		
		JButton deleteButton = new JButton("删除");
		deleteButton.setFont(new Font("宋体", Font.BOLD, 25));
		deleteButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent evt) {
				bookDelteActionPerformed(evt);
			}
		});
		
		JLabel status = new JLabel("借阅状态：");
		status.setFont(new Font("宋体", Font.BOLD, 25));
		
		notBorrowedJrb = new JRadioButton("在馆");
		notBorrowedJrb.setFont(new Font("宋体", Font.BOLD, 25));
		notBorrowedJrb.setSelected(true);
		buttonGroup_1.add(notBorrowedJrb);
		
		borrowedJrb = new JRadioButton("已借");
		borrowedJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup_1.add(borrowedJrb);
		GroupLayout gl_bookMaintainPanel = new GroupLayout(bookMaintainPanel);
		gl_bookMaintainPanel.setHorizontalGroup(
			gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookMaintainPanel.createSequentialGroup()
					.addGap(27)
					.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_bookMaintainPanel.createSequentialGroup()
							.addGap(142)
							.addComponent(alterButton_1)
							.addGap(246)
							.addComponent(deleteButton))
						.addGroup(gl_bookMaintainPanel.createSequentialGroup()
							.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.TRAILING, false)
								.addGroup(gl_bookMaintainPanel.createSequentialGroup()
									.addComponent(bookDesc)
									.addPreferredGap(ComponentPlacement.RELATED)
									.addComponent(bookDescTxt))
								.addGroup(Alignment.LEADING, gl_bookMaintainPanel.createSequentialGroup()
									.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING, false)
										.addGroup(gl_bookMaintainPanel.createSequentialGroup()
											.addComponent(price)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(priceTxt))
										.addGroup(gl_bookMaintainPanel.createSequentialGroup()
											.addComponent(id)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, 132, GroupLayout.PREFERRED_SIZE)))
									.addGap(46)
									.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_bookMaintainPanel.createSequentialGroup()
											.addComponent(bookName)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, 238, GroupLayout.PREFERRED_SIZE))
										.addGroup(gl_bookMaintainPanel.createSequentialGroup()
											.addComponent(author)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, 208, GroupLayout.PREFERRED_SIZE)))
									.addPreferredGap(ComponentPlacement.RELATED)
									.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_bookMaintainPanel.createSequentialGroup()
											.addComponent(bookType)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, 158, GroupLayout.PREFERRED_SIZE)
											.addGap(50))
										.addGroup(Alignment.TRAILING, gl_bookMaintainPanel.createSequentialGroup()
											.addComponent(sex)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(manJrb)
											.addGap(29)))
									.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING)
										.addGroup(gl_bookMaintainPanel.createSequentialGroup()
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(status)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(notBorrowedJrb)
											.addPreferredGap(ComponentPlacement.RELATED)
											.addComponent(borrowedJrb))
										.addComponent(femaleJrb))))
							.addGap(49)))
					.addContainerGap())
		);
		gl_bookMaintainPanel.setVerticalGroup(
			gl_bookMaintainPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookMaintainPanel.createSequentialGroup()
					.addGap(22)
					.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(id)
						.addComponent(idTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookName)
						.addComponent(bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(femaleJrb)
						.addComponent(manJrb)
						.addComponent(sex))
					.addGap(18)
					.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(price)
						.addComponent(priceTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(author)
						.addComponent(authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(bookType)
						.addComponent(bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(status)
						.addComponent(notBorrowedJrb)
						.addComponent(borrowedJrb))
					.addGap(18)
					.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(bookDesc)
						.addComponent(bookDescTxt, GroupLayout.PREFERRED_SIZE, 128, GroupLayout.PREFERRED_SIZE))
					.addPreferredGap(ComponentPlacement.RELATED, 24, Short.MAX_VALUE)
					.addGroup(gl_bookMaintainPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(deleteButton)
						.addComponent(alterButton_1))
					.addContainerGap())
		);
		bookMaintainPanel.setLayout(gl_bookMaintainPanel);
		
		JLabel s_bookName = new JLabel("图书名称：");
		s_bookName.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_bookNameTxt = new JTextField();
		s_bookNameTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_bookNameTxt.setColumns(10);
		
		JLabel s_author = new JLabel("图书作者：");
		s_author.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_authorTxt = new JTextField();
		s_authorTxt.setFont(new Font("宋体", Font.BOLD, 25));
		s_authorTxt.setColumns(10);
		
		JLabel s_bookType = new JLabel("图书类别：");
		s_bookType.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_bookTypeJcb = new JComboBox();
		s_bookTypeJcb.setFont(new Font("宋体", Font.BOLD, 25));
		
		JButton searchButton = new JButton("查询");
		searchButton.setFont(new Font("宋体", Font.BOLD, 25));
		searchButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				bookSearchActionPerformed(e);
			}
		});
		
		JLabel s_status = new JLabel("借阅状态：");
		s_status.setFont(new Font("宋体", Font.BOLD, 25));
		
		s_notBorrowedJrb = new JRadioButton("在馆");
		s_notBorrowedJrb.setBackground(new Color(250, 235, 215));
		s_notBorrowedJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup_2.add(s_notBorrowedJrb);
		s_notBorrowedJrb.setSelected(true);
		
		s_borrowedJrb = new JRadioButton("已借");
		s_borrowedJrb.setBackground(new Color(250, 235, 215));
		s_borrowedJrb.setFont(new Font("宋体", Font.BOLD, 25));
		buttonGroup_2.add(s_borrowedJrb);
		GroupLayout gl_bookSearchPanel = new GroupLayout(bookSearchPanel);
		gl_bookSearchPanel.setHorizontalGroup(
			gl_bookSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookSearchPanel.createSequentialGroup()
					.addComponent(s_bookName)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_bookNameTxt, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE)
					.addGap(32)
					.addComponent(s_author)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_authorTxt, GroupLayout.PREFERRED_SIZE, 115, GroupLayout.PREFERRED_SIZE)
					.addGap(33)
					.addComponent(s_bookType)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(s_bookTypeJcb, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)
					.addGap(18)
					.addComponent(s_status)
					.addPreferredGap(ComponentPlacement.RELATED, 26, Short.MAX_VALUE)
					.addComponent(s_notBorrowedJrb)
					.addGap(18)
					.addComponent(s_borrowedJrb)
					.addGap(18)
					.addComponent(searchButton)
					.addContainerGap())
		);
		gl_bookSearchPanel.setVerticalGroup(
			gl_bookSearchPanel.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_bookSearchPanel.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_bookSearchPanel.createParallelGroup(Alignment.BASELINE)
						.addComponent(s_bookName)
						.addComponent(s_bookNameTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_author)
						.addComponent(s_authorTxt, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_bookType)
						.addComponent(s_bookTypeJcb, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
						.addComponent(s_status)
						.addComponent(searchButton)
						.addComponent(s_borrowedJrb)
						.addComponent(s_notBorrowedJrb))
					.addContainerGap(26, Short.MAX_VALUE))
		);
		bookSearchPanel.setLayout(gl_bookSearchPanel);
		
		bookTable = new JTable();
		bookTable.setBackground(new Color(250, 235, 215));
		
		bookTable.setBackground(new Color(250, 235, 215));//设置表格背景颜色
		bookTable.getTableHeader().setFont(new Font("微软雅黑",Font.BOLD,20));//设置表格开头字体
		bookTable.getTableHeader().setBackground(new Color(127, 255, 212));//设置表格开头背景颜色
		bookTable.setFont(new Font("楷体", Font.BOLD, 22));//设置表格内容字体
		bookTable.setForeground(new Color(0,0,0));//设置表格内容字体颜色		
		bookTable.setRowHeight(bookTable.getRowHeight()+20);//设置表格内容的行高
		
		bookTable.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent met) {
				bookTableMousePressed(met);
				
			}
		});
		bookTable.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"\u7F16\u53F7", "\u56FE\u4E66\u540D\u79F0", "\u56FE\u4E66\u4F5C\u8005", "\u4F5C\u8005\u6027\u522B", "\u56FE\u4E66\u4EF7\u683C", "\u56FE\u4E66\u63CF\u8FF0", "\u56FE\u4E66\u7C7B\u522B", "\u501F\u9605\u72B6\u6001"
			}
		) {
			boolean[] columnEditables = new boolean[] {
				false, false, false, false, false, false, false, false
			};
			public boolean isCellEditable(int row, int column) {
				return columnEditables[column];
			}
		});
		bookTable.getColumnModel().getColumn(5).setPreferredWidth(145);
		bookScrollPane.setViewportView(bookTable);
		getContentPane().setLayout(groupLayout);

		
		//设置文本域边框
		bookDescTxt.setBorder(new LineBorder(new java.awt.Color(127,157,185),1,false));
				
		

		//设置JFrame居中显示
		this.setLocation(200, 0);
		
		this.fillBookType("search");
		
		this.fillBookType("modify");
		
		this.fillTable(new Book());
	}
	
	/**
	 * 图书删除事件处理
	 * @param evt
	 */
	private void bookDelteActionPerformed(ActionEvent evt) {
		String id=idTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择要删除的记录!");
			return;
		}
		int n=JOptionPane.showConfirmDialog(null, "确定要删除该条记录吗？");
		if(n==0) {
			Connection con=null;
			try {
				con=dbUtil.getCon();
				int deleteNum=bookDao.delete(con, id);
				if(deleteNum==1) {
					JOptionPane.showMessageDialog(null, "删除成功!");
					this.reSetValue();
					this.fillTable(new Book());
					borrowImfDao.delete(con, id);
				}
				else {
					JOptionPane.showMessageDialog(null, "删除失败!");
				}
			}catch(Exception e) {
				e.printStackTrace();
			}finally {
				try {
					dbUtil.closeCon(con);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
	}

	/**
	 * 图书修改事件时间处理
	 * @param evt
	 */
	private  void bookUpdateActionPerformed(ActionEvent evt) {
		String id=this.idTxt.getText();
		if(StringUtil.isEmpty(id)) {
			JOptionPane.showMessageDialog(null, "请选择要修改的记录!");
			return;
		}
		String bookName=this.bookNameTxt.getText();
		String author=this.authorTxt.getText();
		String price=this.priceTxt.getText();
		String bookDesc=this.bookDescTxt.getText();
		
		if(StringUtil.isEmpty(bookName)) {
			JOptionPane.showMessageDialog(null, "图书名称不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(author)) {
			JOptionPane.showMessageDialog(null, "图书作者不能为空!");
			return;
		}
		
		if(StringUtil.isEmpty(price)) {
			JOptionPane.showMessageDialog(null, "图书价格不能为空!");
			return;
		}
		
		String sex="";
		if(manJrb.isSelected()) {
			sex="男";
			
		}
		else if(femaleJrb.isSelected()) {
			sex="女";
		}
		
		String status="";
		if(notBorrowedJrb.isSelected()) {
			status="在馆";
			
		}
		else if(borrowedJrb.isSelected()) {
			status="已借";
		}
		BookType bookType=(BookType) bookTypeJcb.getSelectedItem();
		int bookTypeId=bookType.getId();
		
		Book book=new Book(Integer.parseInt(id),bookName,author,sex,Float.parseFloat(price),bookTypeId,bookDesc,status);
		Connection con=null;
		try {
			con=dbUtil.getCon();
			int addNum=bookDao.update(con, book);
			if(addNum==1) {
				JOptionPane.showMessageDialog(null, "图书修改成功!");
				reSetValue();
				this.fillTable(new Book());
				
			}
			else {
				JOptionPane.showMessageDialog(null, "图书修改失败!");				
				
			}
			
		}catch(Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "图书修改失败!");		
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	/**
	 * 表格行点击事件处理
	 * @param me
	 */
	private void bookTableMousePressed(MouseEvent met) {
		int row=this.bookTable.getSelectedRow();
		this.idTxt.setText((String)bookTable.getValueAt(row, 0));
		this.bookNameTxt.setText((String)bookTable.getValueAt(row, 1));
		this.authorTxt.setText((String)bookTable.getValueAt(row, 2));
		String sex=(String)bookTable.getValueAt(row, 3);
		if("男".equals(sex)) {
			this.manJrb.setSelected(true);
		}else if("女".equals(sex)) {
			this.femaleJrb.setSelected(true);
		}
		
		this.priceTxt.setText((Float)bookTable.getValueAt(row, 4)+"");
		this.bookDescTxt.setText((String)bookTable.getValueAt(row, 5));
		String bookTypeName=(String)this.bookTable.getValueAt(row, 6);
		int n=this.bookTypeJcb.getItemCount();
		for(int i=0;i<n;i++) {
			BookType item=(BookType)this.bookTypeJcb.getItemAt(i);
			if(item.getBookTypeName().equals(bookTypeName)) {
				this.bookTypeJcb.setSelectedIndex(i);
			}
		}
		
		String status=(String)bookTable.getValueAt(row, 7);
		if("在馆".equals(status)) {
			this.notBorrowedJrb.setSelected(true);
		}else if("已借".equals(status)) {
			this.borrowedJrb.setSelected(true);
		}
	}
	
	/**
	 * 重置表单
	 */
	private void reSetValue() {
		this.idTxt.setText("");
		this.bookNameTxt.setText("");
		this.authorTxt.setText("");
		this.priceTxt.setText("");
		this.manJrb.setSelected(true);
		this.bookDescTxt.setText("");
		if(this.bookTypeJcb.getItemCount()>0) {
			this.bookTypeJcb.setSelectedIndex(0);
			
		}
	}

	/**
	 * 图书查询事件处理
	 * @param e
	 */
	private void bookSearchActionPerformed(ActionEvent evt) {
		String bookName=this.s_bookNameTxt.getText();
		String author=this.s_authorTxt.getText();
		
		BookType bookType=(BookType) this.s_bookTypeJcb.getSelectedItem();
		int bookTypeId=bookType.getId();
		
		String status="";
		if(s_notBorrowedJrb.isSelected()) {
			status="在馆";
			
		}
		else if(s_borrowedJrb.isSelected()) {
			status="已借";
		}
		
		Book book=new Book(bookName,author,bookTypeId,status);
		this.fillTable(book);
		
		
	}

	/**
	 * 初始下拉框
	 * type 下拉框类型
	 */
	private void fillBookType(String type) {
		Connection con=null;
		BookType bookType=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookTypeDao.list(con, new BookType());
			if("search".equals(type)) {
				bookType=new BookType();
				bookType.setBookTypeName("请选择...");
				bookType.setId(-1);
				this.s_bookTypeJcb.addItem(bookType);
			}
			while(rs.next()) {
				bookType=new BookType();
				bookType.setBookTypeName(rs.getString("bookTypeName"));
				bookType.setId(rs.getInt("id"));
				if("search".equals(type)) {
					this.s_bookTypeJcb.addItem(bookType);
				}
				else if("modify".equals(type)) {
					this.bookTypeJcb.addItem(bookType);
					
				}
			}
			
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 初始化表格数据
	 * @param book
	 */
	private void fillTable(Book book) {
		DefaultTableModel dtm=(DefaultTableModel) bookTable.getModel();
		dtm.setRowCount(0);//设置成0行
		Connection con=null;
		try {
			con=dbUtil.getCon();
			ResultSet rs=bookDao.list(con, book);
			while(rs.next()) {
				Vector v=new Vector();
				v.add(rs.getString("id"));
				v.add(rs.getString("bookName"));
				v.add(rs.getString("author"));
				v.add(rs.getString("sex"));
				v.add(rs.getFloat("price"));
				v.add(rs.getString("bookDesc"));
				v.add(rs.getString("bookTypeName"));
				v.add(rs.getString("status"));
				dtm.addRow(v);
			}
		}catch(Exception e) {
			e.printStackTrace();
		}finally {
			try {
				dbUtil.closeCon(con);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
